﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using System.Xml.Linq;
using MyPortfolio.Models;
using HHModel;
using System.Collections.Generic;
using System.Data.SqlClient;


namespace MyPortfolio.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomePageModel model = new HomePageModel();
            model.News = this.GetTop3News();
            ViewBag.MetaDescription = "和桥互生";
            ViewBag.MetaKeywords = "和桥互生";

            return View("NewView", model);
        }
        public ActionResult ArtistSearch()
        {
            ViewBag.Title = "Simular Music Artist Search | Marcell's Demo";
            return View();
        }
        /// <summary>
        /// Gets the artist name from a form post
        /// </summary>
        /// <param name="artistname">The name of the Artist to search.</param>
        /// <returns>Returns XML from last.fm API web request.</returns>
        /// 
        [HttpPost]
        public string Search(string artistName)
        {
            Artist artist = new Artist();
            var retArtist = artist.ArtistSearch(artistName);
            return retArtist;
        }

        private IList<News> GetTop3News()
        {
            IList<News> news = new List<News>();
            SqlParameter[] parameters = {
					new SqlParameter("@tbname", SqlDbType.NVarChar,50),
					new SqlParameter("@FieldKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@NowPage", SqlDbType.Int,4),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
					new SqlParameter("@FieldShow", SqlDbType.NVarChar,1000),
					new SqlParameter("@FieldOrder", SqlDbType.NVarChar,1000),
					new SqlParameter("@Where", SqlDbType.NVarChar,1000),
                    new SqlParameter("@Totalcount", SqlDbType.Int,4)};
            parameters[0].Value = "News";
            parameters[1].Value = "ID";
            parameters[2].Value = 1;
            parameters[3].Value = 3;
            parameters[4].Value = "";
            parameters[5].Value = "Sort desc,PublishTime desc";
            parameters[6].Value = "1=1";
            parameters[7].Direction = ParameterDirection.Output;
            SqlDataReader reader = HHDBUtility.DbHelperSQL.RunProcedure("P_Pageing", parameters);
            while (reader.Read())
            {
                News newsInfo = new News();
                newsInfo.ID = (int)reader["ID"];
                newsInfo.Title = (string)reader["Title"];
                newsInfo.NewSource = (string)reader["NewSource"];
                //add other fields if needed
                news.Add(newsInfo);
            }

            return news;
        }
    }
}
